package com.bwda.dsrs.systemservice.config;

import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.pwdrule.PwdRuleCondition;
import com.bwda.dsrs.systemservice.domain.po.pwdrule.PwdRulePo;
import com.bwda.dsrs.systemservice.domain.vo.user.UserLoginVo;
import com.bwda.dsrs.systemservice.service.LoginRuleService;
import com.bwda.dsrs.systemservice.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Scheduler配置类
 * @author youzhen
 * @date 2018年6月6日
 */
@Component
public class SchedulerConfig {

    private static Logger log = LoggerFactory.getLogger(SchedulerConfig.class);

    @Autowired
    private UserService userService;

    @Autowired
    private LoginRuleService loginRuleService;

    /**
     * 定时任务
     * @Scheduled(fixedRate=5000)
     * @return
     */
    @Scheduled(cron = "0 01 02 ? * *")
    public void loginTasks() {
        try{
            log.info("Scheduler: 任务启动");
            List<UserLoginVo> userLoginVoList = userService.queryLockTime();
            if(!userLoginVoList.isEmpty()){
                Date nowTime = new Date();
                List<PwdRulePo> poList = loginRuleService.queryList(new PwdRuleCondition());

                if(!poList.isEmpty()){
                    int lockDay = poList.get(0).getLockTime();
                    for(UserLoginVo userLoginVo : userLoginVoList){
                        Date loginTime = userLoginVo.getLockDate();
                        if(nowTime.after(loginTime)){
                            int day = differentDays(loginTime, nowTime);
                            if(day >= lockDay){
                                userService.updateUserState(userLoginVo.getUserId());
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("SchedulerConfig: 定时器异常");
        }
     }


    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2) {
        if(date1.after(date2)){
            Date date3=date1;
            date1=date2;
            date2=date3;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2){
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++){
                int a = i%4;
                int b = i%100;
                int c = i%400;
                boolean res = (a ==0 && b !=0);
                if(res || c==0) {
                    timeDistance += 366;
                }else {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2-day1) ;
        }else {
            return day2-day1;
        }
    }


}
